//请求工具 cnpm i -S axios
import axios from 'axios'

//声明axios
declare module 'axios' {
    interface AxiosResponse<T = any> {
        code: null,
        // 这里追加你的参数
        //  total:number
    }
    export function create(config?: AxiosRequestConfig): AxiosInstance;
}
//创建一个服务
const serve = axios.create({
    // baseURL:'http://localhost:8080',
    baseURL: 'https://reactapi.iynn.cn/',
    timeout: 5000,
})
//请求拦截
serve.interceptors.request.use(config => {
    
    let jwt = localStorage.getItem("jwt")
   if (jwt) {
     config.headers.Authorization = jwt
     }
     
    return config
}, error => Promise.reject(error))
//响应拦截
serve.interceptors.response.use(res => {

    console.log(res);

    if (res.data.context && res.data.context.jwt) {
        localStorage.setItem("jwt", res.data.context.jwt)
    }

    if (res.data.context && res.data.context.acl) {
        localStorage.setItem("acl", JSON.stringify(res.data.context.acl))
    }
    return res.data
   
}, error => Promise.reject(error))

export default serve